﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.Practices.Unity;
using Interface;
using DataAccess;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;

namespace Unity2._1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //代码注册
        private void button1_Click(object sender, EventArgs e)
        {
            //1.注册了一个容器；            
            IUnityContainer container = new UnityContainer();
            //2.向容器中注册IPageDAO服务,并告诉容器用PageDAO实现这个服务           
            container.RegisterType<Interface.IPageDAO1, DataAccess.PageDAO1>();
            Interface.IPageDAO1 page1 = container.Resolve<Interface.IPageDAO1>();
            MessageBox.Show(page1.GetPageByPageID(111).ToString());

            //单例模式的注册
            container.RegisterType<Interface.IPageDAO2, DataAccess.PageDAO2>(new ContainerControlledLifetimeManager());
            Interface.IPageDAO2 page2 = container.Resolve<Interface.IPageDAO2>();
            MessageBox.Show(page2.GetPageByPageID(111).ToString());

            //注册时附带key,同配置文件里面的name
            container.RegisterType<Interface.IPageDAO4, DataAccess.PageDAO4_1>("SQL");
            container.RegisterType<Interface.IPageDAO4, DataAccess.PageDAO4_1>("ORACLE");
            IEnumerable<Interface.IPageDAO4> databases = container.ResolveAll<Interface.IPageDAO4>();
            Interface.IPageDAO4 page4_1 = container.Resolve<Interface.IPageDAO4>("SQL");
            MessageBox.Show(page4_1.GetPageByPageID(111).ToString());
            Interface.IPageDAO4 page4_2 = container.Resolve<Interface.IPageDAO4>("ORACLE");
            MessageBox.Show(page4_2.GetPageByPageID(111).ToString());
            

            //构造函数带参数注册          
            container.RegisterType<Interface.IParDAO, DataAccess.Par>(new InjectionConstructor("a"));
            Interface.IParDAO par = container.Resolve<Interface.IParDAO>();
            MessageBox.Show(par.GetPageByPageID());
            //重载构造函数的参数i为aaa
            Interface.IParDAO par1 = container.Resolve<Interface.IParDAO>(new ParameterOverride("i", "aaa"));
            MessageBox.Show(par1.GetPageByPageID());
        }

        /// <summary>
        /// 从默认配置文件里面读取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //第一种，我们使用configurationManager：引用命名空间：System.Configuration
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection) System.Configuration.ConfigurationManager.GetSection("unity");
            section.Configure(container, "containerOne");
            IPageDAO1 page = container.Resolve<IPageDAO1>();
            MessageBox.Show(page.GetPageByPageID(111).ToString());

            //第二种，我们可以直接用容器读取配置信息：
            IUnityContainer myContainer = new UnityContainer();
            myContainer.LoadConfiguration("containerOne");
            Interface.IPageDAO1 page1 = container.Resolve<Interface.IPageDAO1>();
            MessageBox.Show(page1.GetPageByPageID(111).ToString());
        }

        /// <summary>
        /// 从单独xml文件里面读取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            IUnityContainer container;
            container = new UnityContainer();
            string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BusinessComponentManager.xml");  
            var map = new ExeConfigurationFileMap { ExeConfigFilename = path }; 
            System.Configuration.Configuration config  
                = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            UnityConfigurationSection section
                = (UnityConfigurationSection)config.GetSection("unity");
            section.Configure(container, "containerOne");

            IPageDAO1 page1 = container.Resolve<IPageDAO1>();
            MessageBox.Show(page1.GetPageByPageID(111).ToString());
        }
        //--------------------华丽分割线----上面是单独demo----下面是抽出为公共类使用demo-----
        private void button4_Click(object sender, EventArgs e)
        {
            IPageDAO1 page1 = (IPageDAO1)BusinessComponentManager.GetInstance<IPageDAO1>();
            MessageBox.Show(page1.GetPageByPageID(111).ToString());
            Interface.IPageDAO4 page4_1 = (IPageDAO4)BusinessComponentManager.GetInstance<IPageDAO4>("page4_1");
            MessageBox.Show(page4_1.GetPageByPageID(111).ToString());
        }

        private void button5_Click(object sender, EventArgs e)
        {
            BusinessComponentManager.AddComponent("Interface.IPageDAO1,Interface", "DataAccess.PageDAO1,DataAccess");
            IPageDAO1 page1 = (IPageDAO1)BusinessComponentManager.GetInstance<IPageDAO1>();
            MessageBox.Show(page1.GetPageByPageID(111).ToString());
            BusinessComponentManager.AddComponent("Interface.IPageDAO1,Interface", "DataAccess.PageDAO1,DataAccess","okok");
            IPageDAO1 page2 = (IPageDAO1)BusinessComponentManager.GetInstance<IPageDAO1>("okok");
            MessageBox.Show(page2.GetPageByPageID(111).ToString());
        }
    }
}
